package net.jlibrary.example.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.annotation.Token;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.example.action.vo.ClassificationAdminActionVo;
import net.jlibrary.example.service.IClassificationManager;

@RestController
@RequestMapping("/example")
public class ClassificationAdminAction {
	@Resource
	private IClassificationManager classificationManager;

	@RequestMapping(value = "/classificationAdminView", method = { RequestMethod.GET })
	public ModelAndView view() {
		return new ModelAndView("/example/classificationAdminView");
	}

	@RequestMapping(value = "/classificationAdminJsonList", method = { RequestMethod.POST })
	public Pagination lists(ClassificationAdminActionVo classification, PaginationParam param) {
		Pagination pagination = classificationManager.listsPageDictionarys(classification, param);
		return pagination;
	}

	@RequestMapping(value = "/classificationAdminJsonDetail", method = { RequestMethod.GET })
	public ClassificationAdminActionVo detail(ClassificationAdminActionVo classification) {
		classification = classificationManager.get(classification);
		return classification;
	}

	@RequestMapping(value = "/classificationAdminJsonAddFormInit", method = { RequestMethod.GET })
	public ClassificationAdminActionVo addFormInit() {
		ClassificationAdminActionVo classification = new ClassificationAdminActionVo();
		classification.setValid(true);
		return classification;
	}

	@RequestMapping(value = "/classificationAdminJsonSave", method = { RequestMethod.POST })
	public ModelMap save(@ModelAttribute("vm") ClassificationAdminActionVo classification) {
		classificationManager.save(classification);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/classificationAdminJsonUpdate", method = { RequestMethod.POST })
	@Token
	public ModelMap update(@ModelAttribute("vm") ClassificationAdminActionVo classification, @Token String token,
			BindingResult result) {
		classificationManager.update(classification);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/classificationAdminJsonDelete", method = { RequestMethod.POST })
	public ModelMap delete(Long id) {
		ClassificationAdminActionVo classification = new ClassificationAdminActionVo();
		classification.setId(id);
		classificationManager.delete(classification);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/classificationAdminJsonRenew", method = { RequestMethod.POST })
	public ModelMap renew(Long id) {
		ClassificationAdminActionVo classification = new ClassificationAdminActionVo();
		classification.setId(id);
		classificationManager.renew(classification);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/classificationAdminJsonData", method = { RequestMethod.GET })
	public List<ModelMap> classificationAdminData() {
		List<ClassificationAdminActionVo> classifications = classificationManager
				.listDictionarys(new ClassificationAdminActionVo());
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (ClassificationAdminActionVo classification : classifications) {
			ModelMap map = new ModelMap();
			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, classification.getPrimaryKey());
			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, classification.getText());
			maps.add(map);
		}
		return maps;
	}
}
